'''
Created on 15/08/2012

@author: Romina
'''
from domain.entity import Entity
from gluon.html import DIV, BR

class Mail(object):
    
    @staticmethod
    def removetags(string):
        '''
         ej:     From/To = "Romina Casal" <romina.casal@osplad.org.ar>
             --> From/To = romina.casal@osplad.org.ar
        '''
        string = string.replace("<", "")
        string = string.replace(">", "")
        string = string.replace('"', "")
        sp = string.split()
        for s in sp:
            string = s
        return string
    
    def __init__(self, Account, From, To, Subject, Fecha, Text, MimeText = "", Images = []):
        self.Account = Account
        self.From = Mail.removetags(From)
        self.To = Mail.removetags(To)
        self.Subject = Subject
        self.Fecha = Fecha
        self.Text = Text
        self.MimeText = MimeText
        self.Images = Images
        
    def to_HTML(self):
        html = DIV(
            DIV("Fecha: ", self.Fecha),
            DIV("De: ", self.From),
            DIV("Asunto: ", self.Subject),
            DIV("Para: ", self.To),
            BR(),
            DIV(self.Text)
        )
        return html
        
class MailBandejaEntrada(Mail, Entity):
    

    @staticmethod
    def deserialize(mail):
        Account = mail.user_server
        ID_mail = mail.id_mail
        Date = mail.Date_
        From = mail.From_
        To = mail.To_
        Subject = mail.Subject_
        Message = mail.Message_
        Fecha_Leido = mail.fecha_leido
        m = MailBandejaEntrada(Account, ID_mail, From, To, Subject, Date, Fecha_Leido, Message)
        m.ID = mail.id
        m.Leido = mail.leido
        return m
        
        
    def __init__(self, Account, ID_mail, From, To, Subject, Fecha, Fecha_Leido, Text):
        Mail.__init__(self, Account, From, To, Subject, Fecha, Text, MimeText = "", Images=[])
        Entity.__init__(self)
        self.ID_mail = ID_mail
        self.Fecha_Leido = Fecha_Leido
        self.Leido = False
        

    

class MailConsulta(MailBandejaEntrada):
    
    
    @staticmethod
    def deserialize(mail):
        Account = mail.user_server
        ID_mail = mail.id_mail
        Date = mail.Date_
        From = mail.From_
        To = mail.To_
        Subject = mail.Subject_
        Message = mail.Message_
        Fecha_Leido = mail.fecha_leido
        m = MailConsulta(mail.consulta, Account, ID_mail, From, To, Subject, Date, Fecha_Leido, Message)
        m.ID = mail.id
        return m
    
    def __init__(self, idConsulta, Account, ID_mail, From, To, Subject, Fecha, Fecha_Leido, Text):
        MailBandejaEntrada.__init__(self, Account, ID_mail, From, To, Subject, Fecha, Fecha_Leido, Text)
        self.idConsulta = idConsulta


class MailRespuesta(Entity, Mail):
    
    
    @staticmethod
    def deserialize(mail):
        Message = mail.Message_
        mail_consulta = mail.mail_consulta
        fecha_envio = mail.fecha_envio
        m = MailRespuesta(mail_consulta, Message, fecha_envio, [])
        m.ID = mail.id
        return m
    
    def __init__(self, mail, Text, fecha_envio, Images = []):
        if isinstance(mail, MailConsulta):
            Mail.__init__(self, mail.Account, mail.To, mail.From, "RE: " + str(mail.Subject), mail.Fecha, Text, "", Images)
        else:
            Mail.__init__(self, "", "", "", "RE: " , fecha_envio, Text, "", Images)

        Entity.__init__(self)
        self.mail_consulta = mail
        pass
    

#    def to_json_data_memory(self):
#        me = dict(
#                id = self.id,
#                id_mail = self.ID_mail,
#                de = self.From,
#                to = self.To,
#                subject = self.Subject,
#                text = self.Text,
#                date = self.Fecha,
#                fecha_descarga = self.Fecha_Leido,
#                icon = self.Leido,
#                sele = False
#        )
#        return MyEncoder().encode(me)
#    
#    def json_enconde(self):
#        return MyEncoder().encode(self)    
        
#class MyEncoder(json.JSONEncoder):
#    
#    def default(self, obj):
#        
#        if isinstance(obj, (datetime.date, datetime.datetime)):
#            return obj.isoformat()
#        elif isinstance(obj, MailBandejaEntrada):
#            d = {}
#            d.update(obj.__dict__)
#            return d
#        else:
#            return json.JSONEncoder.default(self, obj)
    
    